home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1995 February: Tool Chest / Dev.CD Feb 95 / Dev.CD Feb 95.toast / Tool Chest / Development Tools & Languages / Dylan Related / Mindy-1.1 (sources only) / mindy-1.1 / interp / misc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-28  |  4.5 KB  |  169 lines  |  [TEXT/ttxt]

  1. /**********************************************************************\
  2. *
  3. *  Copyright (c) 1994  Carnegie Mellon University
  4. *  All rights reserved.
  5. *  
  6. *  Use and copying of this software and preparation of derivative
  7. *  works based on this software are permitted, including commercial
  8. *  use, provided that the following conditions are observed:
  9. *  
  10. *  1. This copyright notice must be retained in full on any copies
  11. *     and on appropriate parts of any derivative works.
  12. *  2. Documentation (paper or online) accompanying any system that
  13. *     incorporates this software, or any part of it, must acknowledge
  14. *     the contribution of the Gwydion Project at Carnegie Mellon
  15. *     University.
  16. *  
  17. *  This software is made available "as is".  Neither the authors nor
  18. *  Carnegie Mellon University make any warranty about the software,
  19. *  its performance, or its conformity to any specification.
  20. *  
  21. *  Bug reports, questions, comments, and suggestions should be sent by
  22. *  E-mail to the Internet address "gwydion-bugs@cs.cmu.edu".
  23. *
  24. ***********************************************************************
  25. *
  26. * $Header: misc.c,v 1.8 94/06/27 16:32:18 wlott Exp $
  27. *
  28. * This file implements the stuff we couldn't think of anyplace
  29. * better to put.
  30. *
  31. \**********************************************************************/
  32.  
  33. #include "mindy.h"
  34. #include "thread.h"
  35. #include "bool.h"
  36. #include "list.h"
  37. #include "vec.h"
  38. #include "func.h"
  39. #include "obj.h"
  40. #include "module.h"
  41. #include "sym.h"
  42. #include "def.h"
  43. #include "num.h"
  44.  
  45. static struct variable *generic_apply_var = NULL;
  46.  
  47.  
  48. static obj_t dylan_exit(obj_t exit_value)
  49. {
  50.     exit(fixnum_value(exit_value));
  51. }
  52.  
  53. static void dylan_values(struct thread *thread, int nargs)
  54. {
  55.     obj_t *args = thread->sp - nargs;
  56.     do_return(thread, args-1, args);
  57. }
  58.  
  59. static void dylan_apply(struct thread *thread, int nargs)
  60. {
  61.     obj_t *args = thread->sp - nargs;
  62.     obj_t *old_sp = args-1;
  63.     obj_t *src = args;
  64.     obj_t *dst = old_sp;
  65.     obj_t *end = thread->sp - 1;
  66.     obj_t seq = *end;
  67.     obj_t class = object_class(seq);
  68.     boolean vector;
  69.  
  70.     if (!(vector = (class == obj_SimpleObjectVectorClass))
  71.     && class != obj_EmptyListClass && class != obj_PairClass) {
  72.     /* It isn't a simple-object-vector nor a list, we have to defer. */
  73.     *dst++ = generic_apply_var->value;
  74.     while (src < end)
  75.         *dst++ = *src++;
  76.     *dst++ = *src;
  77.     }
  78.     else {
  79.     /* Copy the function and the first n-1 args down the stack. */
  80.     while (src < end)
  81.         *dst++ = *src++;
  82.  
  83.     /* Spread the collection out on the stack. */
  84.     if (vector) {
  85.         src = obj_ptr(struct sovec *, seq)->contents;
  86.         end = src + obj_ptr(struct sovec *, seq)->length;
  87.         while (src < end)
  88.         *dst++ = *src++;
  89.     }
  90.     else {
  91.         while (seq != obj_Nil) {
  92.         *dst++ = HEAD(seq);
  93.         seq = TAIL(seq);
  94.         }
  95.     }
  96.     }
  97.     thread->sp = dst;
  98.     invoke(thread, dst - args);
  99. }
  100.  
  101. static void dylan_apply_curry(struct thread *thread, int nargs)
  102. {
  103.     obj_t *args = thread->sp - 3;
  104.     obj_t func = args[0];
  105.     obj_t vec1 = args[1];
  106.     obj_t vec2 = args[2];
  107.     int len1 = SOVEC(vec1)->length;
  108.     int len2 = SOVEC(vec2)->length;
  109.     int i;
  110.  
  111.     assert(nargs == 3);
  112.  
  113.     args[-1] = func;
  114.  
  115.     for (i = 0; i < len1; i++)
  116.     *args++ = SOVEC(vec1)->contents[i];
  117.     for (i = 0; i < len2; i++)
  118.     *args++ = SOVEC(vec2)->contents[i];
  119.  
  120.     thread->sp = args;
  121.  
  122.     invoke(thread, len1+len2);
  123. }
  124.  
  125.  
  126. /* Invoking the debugger. */
  127.  
  128. static void dylan_invoke_debugger(struct thread *thread, int nargs)
  129. {
  130.     obj_t *args;
  131.  
  132.     assert(nargs == 1);
  133.  
  134.     args = thread->sp - 1;
  135.     push_linkage(thread, args);
  136.  
  137.     thread_debuggered(thread, args[0]);
  138. }
  139.  
  140.  
  141. /* Init stuff. */
  142.  
  143. void init_misc_functions(void)
  144. {
  145.     define_generic_function("main", 0, TRUE, obj_False, FALSE,
  146.                 obj_Nil, obj_ObjectClass);
  147.     define_function("raw-exit", list1(obj_IntegerClass), FALSE, obj_False,
  148.             FALSE, obj_ObjectClass, dylan_exit);
  149.     define_constant("invoke-debugger",
  150.             make_raw_function("invoke-debugger", 1, FALSE, obj_False,
  151.                       FALSE, obj_Nil, obj_ObjectClass,
  152.                       dylan_invoke_debugger));
  153.     define_constant("values",
  154.             make_raw_function("values", 0, TRUE, obj_False, FALSE,
  155.                       obj_Nil, obj_ObjectClass,
  156.                       dylan_values));
  157.     define_constant("apply",
  158.             make_raw_function("apply", 2, TRUE, obj_False, FALSE,
  159.                       obj_Nil, obj_ObjectClass,
  160.                       dylan_apply));
  161.     generic_apply_var = find_variable(module_BuiltinStuff,
  162.                       symbol("generic-apply"),
  163.                       FALSE, TRUE);
  164.     define_constant("apply-curry",
  165.             make_raw_function("apply-curry", 3, FALSE, obj_False,
  166.                       FALSE, obj_Nil, obj_ObjectClass,
  167.                       dylan_apply_curry));
  168. }
  169.